package com.android.tnovoselec.travelassistant.overlay;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.location.Location;
import android.util.DisplayMetrics;

import com.android.tnovoselec.travelassistant.R;
import com.android.tnovoselec.travelassistant.Util;
import com.android.tnovoselec.travelassistant.dto.BankomatDto;
import com.nth.android.ar.ARAugmentedView;
import com.nth.android.ar.ARCamera;
import com.nth.android.ar.ARItemizedOverlay;
import com.nth.android.ar.AROverlayItem;
import com.nth.android.ar.Vector3;

public class BankomatiAROverlayNavigation extends ARItemizedOverlay {

	private ARAugmentedView arView;
	private Context context;
	private List<AROverlayItem> items;
	private List<BankomatDto> poiARList;
	private Hashtable<String, Bitmap> infoBitmapTable;

	private Paint textPaint;
	private Paint textBoldPaint;

	private final int PADDING_TOP = 30;
	private final int PADDING_RIGHT = 25;
	private final int PADDING_LEFT = 15;
	private final int PADDING_LEFT_DISTANCE = 175;

	private int[] xPos;
	private AROverlayItem item;
	private Vector3 markerPosition;
	private String distanceString;
	private String titleString;
	private String descriptionString;
	private String bankID;
	// private Bitmap infoBitmap, infoYellow;

	private int densityDpi;
	private float scaleFactor;
	private int numARPois = 3;

	public BankomatiAROverlayNavigation(ARAugmentedView arView) {
		super(arView.getContext(), R.drawable.bubble_hi);

		this.arView = arView;
		context = arView.getContext();
		items = new ArrayList<AROverlayItem>();

		// infoYellow = BitmapFactory.decodeResource(context.getResources(),
		// R.drawable.info_yellow);
		infoBitmapTable = new Hashtable<String, Bitmap>();

		densityDpi = context.getResources().getDisplayMetrics().densityDpi;
		scaleFactor = (float) densityDpi / DisplayMetrics.DENSITY_HIGH;
		if (densityDpi == DisplayMetrics.DENSITY_LOW) {
			numARPois = 2;
		}

		textPaint = new Paint();
		textPaint.setColor(Color.WHITE);
		textPaint.setTextSize(20 * scaleFactor);
		textPaint.setAntiAlias(true);
		textPaint.setStyle(Paint.Style.FILL);
		textPaint.setTypeface(Typeface.DEFAULT);

		textBoldPaint = new Paint(textPaint);
		textBoldPaint.setColor(Color.BLACK);
		textBoldPaint.setTextSize(20 * scaleFactor);
		textBoldPaint.setTypeface(Typeface.DEFAULT_BOLD);
	}

	public void setPoiARList(List<BankomatDto> poiARList) {
		items.clear();
		this.poiARList = poiARList;

		for (BankomatDto poi : poiARList) {
			AROverlayItemNavigation item = new AROverlayItemNavigation(poi.getAdresa(), poi.getGrad(), getLocation(poi.getLatitude(), poi.getLongitude()), poi);
			items.add(item);
		}
	}

	@Override
	protected AROverlayItem getItem(int index) {
		return items.get(index);
	}

	@Override
	protected int size() {
		return items.size();
	}

	@Override
	protected boolean onTap(int index) {
		if (getItem(index) instanceof AROverlayItemNavigation) {

			return true;
		}
		return false;
	}

	@Override
	public void locationUpdated(Location location) {
		super.locationUpdated(location);
		Collections.sort(items);
	}

	@Override
	protected void calcAndDraw(Canvas canvas, ARCamera viewCam, float addX, float addY, float curBearing) {
		int numVisible = 0;
		xPos = new int[size()];
		for (int i = 0; i < size(); i++) {
			item = getItem(i);
			item.calc(viewCam, addX, addY);

			xPos[i] = Integer.MAX_VALUE;
			if (item.isVisible()) {
				markerPosition = item.getcMarker();
				if (markerPosition.x > -getItemBackground().getWidth() / 2 && markerPosition.x < canvas.getWidth() + getItemBackground().getWidth() / 2) {
					titleString = item.getTitle();
					int titleBreakCharIndex = textPaint.breakText(titleString, true, getItemBackground().getWidth() - PADDING_LEFT * 2 - 5, null);
					if (titleBreakCharIndex < titleString.length()) {
						titleString = titleString.substring(0, titleBreakCharIndex).trim() + "...";
					}

					descriptionString = item.getDescription();
					int descBreakCharIndex = textPaint.breakText(descriptionString, true, getItemBackground().getWidth() - PADDING_LEFT * 2 - 5, null);
					if (descBreakCharIndex < descriptionString.length()) {
						descriptionString = descriptionString.substring(0, descBreakCharIndex).trim() + "...";
					}

					int distance = (int) item.getDistance();
					if (distance > 1000) {
						distanceString = distance / 1000 + " km";
					} else {
						distanceString = distance + " m";
					}
					bankID = Util.getARTypeName(((AROverlayItemNavigation)item).getPoi().getBank_id());
				

					canvas.save();

					markerPosition.y = arView.getHeight() - getItemBackground().getHeight() * numVisible - 75 * scaleFactor;
					canvas.translate(markerPosition.x, markerPosition.y);
					canvas.drawBitmap(getItemBackground(), -getItemBackground().getWidth() / 2, -getItemBackground().getHeight(), null);
					canvas.drawText(titleString, -getItemBackground().getWidth() / 2 + PADDING_LEFT * scaleFactor, -getItemBackground().getHeight()
							+ PADDING_TOP * scaleFactor, textPaint);
					canvas.drawText(descriptionString, -getItemBackground().getWidth() / 2 + PADDING_LEFT * scaleFactor, -getItemBackground().getHeight()
							+ PADDING_TOP * scaleFactor + textPaint.getTextSize(), textPaint);
					canvas.drawText(bankID, -getItemBackground().getWidth() / 2 + PADDING_LEFT * scaleFactor, -getItemBackground().getHeight()
							+ (PADDING_TOP + 65) * scaleFactor, textBoldPaint);
					canvas.drawText(distanceString, -getItemBackground().getWidth() / 2 + PADDING_LEFT_DISTANCE * scaleFactor, -getItemBackground().getHeight()
							+ (PADDING_TOP + 65) * scaleFactor, textPaint);
					canvas.restore();

					numVisible++;
				}
				if (numVisible >= numARPois) {
					break;
				}
				xPos[i] = (int) markerPosition.x;
			}
		}
	}

	private static Location getLocation(double latitude, double longitude) {
		Location location = new Location("AR");
		location.setLatitude(latitude);
		location.setLongitude(longitude);
		return location;
	}

	private class AROverlayItemNavigation extends AROverlayItem {

		private BankomatDto poi;

		public AROverlayItemNavigation(String title, String description, Location location, BankomatDto poi) {
			super(title, description, location);
			this.poi = poi;
		}

		public BankomatDto getPoi() {
			return poi;
		}

	}

}
